#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int wardrobeFinishing(int m, int n, int cnt) {
        function<bool(int, int)>
            jug = [&](int r, int c)->bool
            {
                int cur = 0;
                while (r)
                {
                    cur += r % 10;
                    r /= 10;
                }
                while (c)
                {
                    cur += c % 10;
                    c /= 10;
                }
                return cur > cnt;
            };

        vector<vector<bool>> check(m, vector<bool>(n, true));
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (jug(i, j)) check[i][j] = false;

        int dx[4] = { 0, 0, -1, 1 };
        int dy[4] = { 1, -1, 0, 0 };
        int ret = 0;
        function<void(int, int)>
            dfs = [&](int r, int c)
            {
                ret++;
                check[r][c] = false;
                for (int i = 0; i < 4; i++)
                {
                    int x = r + dx[i];
                    int y = c + dy[i];
                    if (x >= 0 && x < m && y >= 0 && y < n && check[x][y])
                        dfs(x, y);
                }
            };

        dfs(0, 0);
        return ret;
    }
};